import { setStorage, getStorage } from './utils/storage'
import './utils/extendApi'

App({
  // 全局共享的数据
  globalData: {
    token: getStorage('token') || '',
    olderInfo: getStorage('olderInfo') || [],
    olderId: 0
  },
  setToken(token) {
    // token本地存储
    setStorage('token', token)
    this.globalData.token = token
  },
  setOlderInfo(olderInfo) {
    olderInfo.forEach((item, index) => {
      if (index === 0) {
        this.globalData.olderId = item.id
        item.checked = true
      } else {
        item.checked = false
      }
    })
    // olderInfo本地存储
    setStorage('olderInfo', olderInfo)
    this.globalData.olderInfo = olderInfo
  },
  setolderId(olderId) {
    this.globalData.olderId = olderId
  },
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function () {},

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (options) {},

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function () {},

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function (msg) {}
})
